/***************************************************************************
*   Copyright (C) 2011 by Chuck Gao & Alex Liu                             *
*                                                                          *
*   This program is free software: you can redistribute it and/or modify   *
*   it under the terms of the GNU General Public License as published by   *
*   the Free Software Foundation, either version 3 of the License, or      *
*   (at your option) any later version.                                    *
*                                                                          *
*   This program is distributed in the hope that it will be useful,        *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
*   GNU General Public License for more details.                           *
*                                                                          *
*   You should have received a copy of the GNU General Public License      *
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
*                                                                          *
*   Contact author: chuckgao.cg@gmail.com                                  *
*                   alex2202375@gmail.com                                  *
***************************************************************************/

import QtQuick 1.0
import Box2D 1.0

Rectangle {
    id: splashScreen
    width: 360
    height: 640
    color: "lightblue"
    clip: true

    Image {
        id: man
        x: 180
        y: 190
        source: "qrc:/Images/man.png"

        Component.onCompleted: {

        }
    }

    Image {
        id: cookingware
        x: 40
        y: 300
        width: 240
        height: 190
        fillMode: Image.PreserveAspectFit
        smooth: true
        source: "qrc:/Images/cookingware.png"

        function startCooking()
        {
            fire1.visible = true
            fire2.visible = true
        }

        Image {
            id: fire1
            x: 60
            y: 45
            width: 45
            height: 45
            visible: false
            smooth: true
            source: "qrc:/Images/fire.png"
        }

        Image {
            id: fire2
            x: 136
            y: 45
            width: 45
            height: 45
            visible: false
            smooth: true
            source: "qrc:/Images/fire.png"
        }
    }

    Image {
        id: pan
        x: 130
        y: 300
        width: 160
        height: 100
        fillMode: Image.PreserveAspectFit
        smooth: true
        source: "qrc:/Images/pan.png"
    }

    Item {
        id: cookingScene
        width: 160
        height: 360
        x: 60
        clip: true

        World {
            id: world;
            anchors.fill: parent

            //            gravity: Qt.point(0,0)

            CookingItem {
                x: 145
                y: 123
                cookingItemImg: "qrc:/Images/carrot.png"
            }

            CookingItem {
                x: 45
                y: 300
                cookingItemImg: "qrc:/Images/chicken.png"
            }

            CookingItem {
                x: 106
                y: 44
                cookingItemImg: "qrc:/Images/chickenLeg.png"
            }

            CookingItem {
                x: 66
                y: 45
                cookingItemImg: "qrc:/Images/hamburger.png"
            }

            CookingItem {
                x: 120
                y: 145
                cookingItemImg: "qrc:/Images/meat.png"
            }

            Body {
                id: ground
                bodyType: Body.Static
                fixtures: Box { anchors.fill: parent }
                height: 10
                anchors { left: parent.left; right: parent.right; top: parent.bottom }
            }

            Body {
                id: leftWall
                bodyType: Body.Static
                fixtures: Box { anchors.fill: parent }
                width: 20
                anchors { right: parent.left; bottom: ground.top; top: parent.top }
            }

            Body {
                id: rightWall
                bodyType: Body.Static
                fixtures: Box { anchors.fill: parent }
                width: 20
                anchors { left: parent.right; bottom: ground.top; top: parent.top }
            }
        }
    }

    Text {
        id: lazywormLogo
        x: 60
        y: 440
        text: "Lazy Worm"
        color: "blue"
        font.family: "Freestyle Script"
        font.pointSize: 32
        font.weight: Font.DemiBold
    }

    Text {
        id: cookingLogo
        x: 160
        anchors.top: lazywormLogo.bottom
        text: "Cooking"
        color: "gray"
        font.family: "Freestyle Script"
        font.pointSize: 32
        font.weight: Font.DemiBold
    }

    Item {
        id: appLoadingProgressBar
        width: 240
        height: 10
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 70
        visible: (1 != progress)
        smooth: true

        property real progress: 0

        /*******************************************
         * Test code -- Enter
         *****************************************/
        property int r: 0 //Math.floor(Math.random() * 2000 + 500 )
        NumberAnimation on progress { duration: appLoadingProgressBar.r; from: 0; to: 1; }
        /*******************************************
         * Test code -- Exit
         *****************************************/

        Rectangle {
            anchors.fill: parent; color: "white"; opacity: 0.7
            radius: 15
            smooth: true

            Rectangle {
                color: "lightgreen";
                width: appLoadingProgressBar.width * appLoadingProgressBar.progress
                height: appLoadingProgressBar.height
                smooth: true
            }
        }
    }

    Item {
        id: tapToCookBtn
        width: 60
        height: 30
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 60
        visible: (1 == appLoadingProgressBar.progress)

        Text {
            anchors.centerIn: parent
            text: "Tap To Cook"
            font.family: "Freestyle Script"
            font.pointSize: 20
            font.weight: Font.DemiBold
            color: Qt.rgba(0,0,0,200)
        }

        NumberAnimation on opacity { duration: 1600; from: 0.3; to: 1; loops: Animation.Infinite }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                // Start to cook, enter the cooking main menu. Run some animation here :)
                cookStartAnimation.running = true
            }
        }
    }

    Timer {
        interval: 100; running: tapToCookBtn.visible ; repeat: false
        onTriggered: {
            cookingware.startCooking()
        }
    }

//    NumberAnimation on x { id: animation1; targets: [cookingware, lazywormLogo]; to: "100"; duration: 600 }
    ParallelAnimation {
        id: cookStartAnimation
        running: false
        onCompleted: {
            mainMenu.visible = true
            mainMenu.showCookingMenu()
            splashScreen.destroy();
        }
        // Fly to left
        NumberAnimation { target: cookingware; properties: "x,y"; to: -200; duration: 600}
        RotationAnimation { target: cookingware; properties: "rotation"; duration: 500; to: 360; }
        NumberAnimation { target: lazywormLogo; properties: "x,y"; to: -200; duration: 600}
        NumberAnimation { target: cookingScene; properties: "x,y"; to: -200; duration: 600}

        // Fly to right
        NumberAnimation { target: man; properties: "x,y"; to: 600; duration: 600}
        RotationAnimation { target: man; properties: "rotation"; duration: 500; to: 360; }
        NumberAnimation { target: cookingLogo; properties: "x,y"; to: 600; duration: 600}
        NumberAnimation { target: pan; properties: "x,y"; to: 600; duration: 600}
        RotationAnimation { target: pan; properties: "rotation"; duration: 500; to: 360; }

        // Opacity
        NumberAnimation { target:splashScreen ; properties: "opacity"; to: 0.4; duration: 600}
    }
}
